package cn.ctbu.simple_score_manage.api;

import cn.ctbu.simple_score_manage.common.R;
import cn.ctbu.simple_score_manage.common.REnum;
import cn.ctbu.simple_score_manage.common.RunException;
import cn.ctbu.simple_score_manage.domain.Score;
import cn.ctbu.simple_score_manage.domain.Student;
import cn.ctbu.simple_score_manage.service.ICourseService;
import cn.ctbu.simple_score_manage.service.IScoreService;
import cn.ctbu.simple_score_manage.service.IStudentService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IScoreService scoreService;
    @Autowired
    private ICourseService courseService;
    @GetMapping("/course/list")
    public R selectUnChoseCourse(@RequestParam String studentNo){
        LambdaQueryWrapper<Score> wrapper = new LambdaQueryWrapper<>();
        wrapper.notLike(Score::getStudentNo,studentNo);
        scoreService.list(wrapper);
        return R.success();
    }
    @PostMapping("/score/change")
    public R changeScore(@RequestBody Score score){
        scoreService.updateById(score);
        return R.success();
    }

    /**
     * 无需传参数,获取学生所上所有成绩的信息
     * @return
     */
    @GetMapping("/score")
    public R getScore(HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer id = (Integer) session.getAttribute("id");
        List<Map<String, String>> scores = studentService.selectScoreList(id);
        return R.success(scores);
    }

    /**
     * 根据id获取学生上的所有成绩
     * @param id
     * @return
     */
    @GetMapping("/score/id")
    public R  getScoreById(@RequestParam Integer id){
        List<Map<String, String>> maps = studentService.selectScoreList(id);
        return R.success(maps);
    }

    /**
     * 按照邮箱和密码进行登录
     * 纪录登录的id和no
     *
     * @param student
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Student student, HttpServletRequest request) throws RunException {
        Student studentReturned = studentService.selectByNoAndPassword(student.getNo(), student.getPassword());
        if (studentReturned != null) {
            studentReturned.setPassword("");
            //存入session
            request.getSession().setAttribute("id", studentReturned.getId());
            request.getSession().setAttribute("studentNo", studentReturned.getNo());
            log.info("学生用户 id:" + studentReturned.getId() + "  no:" + studentReturned.getNo() + "  登录");
            return R.success(studentReturned);
        } else throw new RunException(REnum.LOGIN_ERROR);
    }

    /**
     * 进行登出,清楚session中的Id和No
     *
     * @param
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        log.info("学生用户 学号:" + request.getSession().getAttribute("studentNo") + "  已经退出");
        //清理session中的员工id和no
        request.getSession().removeAttribute("id");
        request.getSession().removeAttribute("studentNo");
        return R.success();
    }

    /**
     * 分页读取全部内容
     *
     * @return
     */
    @GetMapping()
    public R<Page<Student>> getByPage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size
            , @RequestParam(defaultValue = "") String name ,@RequestParam(defaultValue = "") Integer sex,@RequestParam(defaultValue = "") String no) {
        page++;
        Page<Student> studentPage = new Page<>(page, size);
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        if (name != null&& !name.equals("")) {
            wrapper.like(Student::getName, name);
        }
        if (sex != null&& sex!=0) {
            wrapper.like(Student::getSex, sex);
        }
        if (no != null&& !no.equals("")) {
            wrapper.like(Student::getNo, no);
        }
        //可在此添加筛选条件
        studentService.page(studentPage, wrapper);
        List<Student> records = studentPage.getRecords();

        for (Student record : records) {
            record.setPassword("");
        }
//        log.info("返回的数据条数为" + studentPage.getSize());
        return R.success(studentPage);
    }
    @GetMapping("/getOne")
    public R getAStudent(@RequestParam Integer id){
        Student byId = studentService.getById(id);
        byId.setPassword("");
        return R.success(byId);
    }

    @PostMapping()
    public R add(@RequestBody Student student) throws RunException {
        //检查是否有重复的学号
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getNo, student.getNo());
        List<Student> list = studentService.list(wrapper);
        if (!list.isEmpty()) {
            throw new RunException(REnum.REPETITIVE_NO);
        }

        studentService.save(student);
        return R.success();
    }

    /**
     * 删除学生
     *
     * @param student
     * @return
     */
    @DeleteMapping()
    public R delete(@RequestBody Student student) {
        studentService.removeById(student.getId());
        log.info("删除了一个学生");
        return R.success();
    }

    /**
     * 进行修改操作
     *
     * @param student
     * @return
     */
    @PutMapping()
    public R update(@RequestBody Student student) {
        log.info(student.toString());
        studentService.updateById(student);
        return R.success();
    }


}
